/**
 * 
 */
package ar.edu.unq.svnpermission.ui.pages;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxFallbackLink;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.Model;

import ar.edu.unq.svnpermission.domain.User;
import ar.edu.unq.svnpermission.panels.UserDetailPanel;
import ar.edu.unq.svnpermission.ui.model.Members;

/**
 * @author nicolas
 */
public class UsersPage extends InternalPage<Members> {
	private static final long serialVersionUID = 1L;
	private ListView<User> usersListView;
	private ModalWindow modalPopup;

	@Override
	protected void addFields() {
		this.addUsersTable();
		this.addDetailModalPopup();
		
	}

	@Override
	protected void addButtons() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected CompoundPropertyModel<Members> createModel() {
		return new CompoundPropertyModel<Members>(new Members());
	}

	@Override
	protected String getFormName() {
		return "formUsersList";
	}
	
	private void addUsersTable()
	{
		usersListView = new ListView<User>("allUsers") {
			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(final ListItem<User> item) {
				item.add(new Label("name",item.getModelObject().getName()));
				item.add(new Label("lastName",item.getModelObject().getLastName()));
				item.add(new Label("email",item.getModelObject().getEmail()));
				item.add(new AjaxFallbackLink<User>("deletePermissionLink",new Model(item.getModelObject())) {
					private static final long serialVersionUID = 1L;

					@Override
					public void onClick(AjaxRequestTarget target) {
						new Members().removeUser(item.getModelObject());
						UsersPage.this.usersListView.modelChanged();
						target.add(UsersPage.this);
					}
				});	
				
				item.add(new AjaxLink<Void>("viewDetail")
				        {
					private static final long serialVersionUID = 1L;

					@Override
		            public void onClick(AjaxRequestTarget target)
		            {
						Members member = (Members) UsersPage.this.getForm().getDefaultModelObject();
						member.setSelectedUser(item.getModelObject());
						modalPopup.setContent(new UserDetailPanel(modalPopup.getContentId(), UsersPage.this, item.getModelObject()));
				        modalPopup.setTitle("El usuario " + member.getSelectedUser() + " pertenece a:");
		                modalPopup.show(target);
		            }
		        });
				
			}
			 
		};
		this.setOutputMarkupId(true);
		this.getForm().setOutputMarkupId(true);
		this.getForm().add(usersListView);


	}	
	
	public void addDetailModalPopup()
	{
        this.modalPopup = new ModalWindow("detailUserPopup",this.getDefaultModel());
        this.modalPopup.setCookieName("permission");
        this.modalPopup.setAutoSize(true);
        this.getForm().add(this.modalPopup);	
	}
	

}
